package com.baizhi.springmvc.controller;

import com.alibaba.fastjson.JSON;

import com.baizhi.springmvc.entity.Cart;
import com.baizhi.springmvc.entity.Item;
import com.baizhi.springmvc.entity.Order;
import com.baizhi.springmvc.entity.User;
import com.baizhi.springmvc.service.AddressService;
import com.baizhi.springmvc.service.ItemService;
import com.baizhi.springmvc.service.OrderService;
import com.baizhi.springmvc.service.imp.AddressServiceImp;
import com.baizhi.springmvc.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Controller
public class OrderController{

@Autowired
private AddressService addressService;
@Autowired
private OrderService orderService;
@Autowired
private ItemService itemService;
    @RequestMapping("/queryAddress")
    public String queryAddress(HttpSession session,Map map) {
        User user = (User)session.getAttribute("user");
        if (user == null) {
            return "forward:/user/login_form.jsp";
        } else {
          List addressList = addressService.selectAllAddress(user.getUser_id());
            map.put("addressList", addressList);
            return "forward:/order/address_form.jsp";
        }
    }
    @RequestMapping("/addOrder")
    public String addOrder(HttpSession session,Integer addressid,Map map) {
        User user = (User)session.getAttribute("user");
        Jedis jedis = RedisUtil.getJedis();
        String c = jedis.get(user.getUser_id()+"");
        Cart cart = JSON.parseObject(c, Cart.class);
        Order order = new Order();
        order.setTotal_price(cart.getTotalPrice());
        order.setAddress_id(addressid);
        order.setUser_id(user.getUser_id());
        order.setOrder_state(0);
        orderService.insert(order);
        //给order——OK页面传参
        Double totalPrice=cart.getTotalPrice();
        map.put("totalPrice", totalPrice);
        //给order——OK页面传参
       Integer order_id=order.getOrder_id();
        map.put("order_id", order_id);
        HashMap<Integer, Item> addmap = cart.getMap();
        Set<Integer> set = addmap.keySet();

        for (Integer i : set) {
            Item item = new Item();
            item.setCount(addmap.get(i).getCount());
            item.setDang_price(addmap.get(i).getBook().getDangPrice());
            item.setBook_id(i);
            item.setOrder_id(order.getOrder_id());
            itemService.insert(item);
        }
        jedis.del(user.getUser_id()+"");
        RedisUtil.ClossJedis(jedis);
        return "forward:/order/order_ok.jsp";
    }
    @RequestMapping("/selectAllOrder")
    public String selectAllOrder(Map map) {
       List orderList = orderService.selectAllOrder();
        map.put("orderList", orderList);
        return "forward:/order/my_order.jsp";
    }
    @RequestMapping("/deleteOrder")
    public String deleteOrder(Integer id) {
        itemService.deleteItem(id);
        orderService.deleteOrder(id);

        return "redirect:/selectAllOrder.do";
    }
    @RequestMapping("/updateOrderStatus")
    public String updateOrderStatus(Integer out_trade_no) {
        orderService.updateOrderStatus(out_trade_no);
        return "redirect:/selectAllOrder.do";
    }

}
